@extends('base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <form class="layui-form" action="{{route("crm.visit")}}">
                <div class="layui-btn-group">
                    @can('crm.visit.destroy')
                        <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" data-url="{{route('crm.visit.destroy')}}" id="listDelete">删除</button>
                    @endcan
                    @can('crm.visit.create')
                        <a class="layui-btn layui-btn-sm" id="addBtn">添加</a>
                    @endcan
                    <button type="button" lay-submit lay-filter="search" class="layui-btn layui-btn-sm" >搜索</button>
                </div>
            </form>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('crm.visit.edit')
                        <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    @endcan
                    @can('crm.visit.destroy')
                        <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</a>
                    @endcan
                </div>
            </script>
        </div>

    </div>
@endsection

@section('script')
    <script>
        layui.use(['layer','table','form','laydate','upload'],function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;
            var upload = layui.upload;
            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable'
                ,height: 'full-140'
                ,url: "{{ route('crm.visit') }}" //数据接口
                ,page: true //开启分页
                ,cols: [[ //表头
                    {checkbox: true}
                    ,{field: 'customer', title: '客户名称',templet:function (d) {
                            return '<a lay-event="show" style="cursor: pointer;color: green" title="点击查看">'+ d.customer.name+ '</a>'
                        }}
                    ,{field: 'stime', title: '开始日期'}
                    ,{field: 'etime', title: '结束日期'}
                    ,{field: 'address', title: '到访地址'}
                    ,{field: 'remark', title: '备注'}
                    ,{field: 'check_status', title: '审核状态',templet:function (d) {
                            if (d.check_status===1){
                                return '<span class="layui-badge" lay-event="check">待审核</span>'
                            }else if (d.check_status===2){
                                return '<span class="layui-badge layui-bg-black" lay-event="check">未通过</span>'
                            }else if (d.check_status===3){
                                return '<span class="layui-badge layui-bg-green" lay-event="check">通过</span>'
                            }
                        },width: 100}
                    ,{field: 'check_result', title: '审核备注'}
                    ,{field: 'created_at', title: '申请时间'}
                    ,{field: 'owner_user_nickname', title: '所属人'}
                    ,{fixed: 'right', width: 160, align:'center', toolbar: '#options', title:'操作'}
                ]]
            });

            //监听工具条
            table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'check'){
                    layer.open({
                        type: 2,
                        title: "审核",
                        shadeClose: true,
                        area: ["600px","600px"],
                        content: '/caiwu/visit/check?id='+data.id,
                    })
                } else if (layEvent === 'show'){
                    layer.open({
                        type: 2,
                        title: "详情",
                        shadeClose: true,
                        area: ["90%","90%"],
                        content: '/crm/customer/'+data.customer_id+'/show',
                    })
                } else if(layEvent === 'edit'){
                    layer.open({
                        type: 2,
                        title: "编辑",
                        shadeClose: true,
                        area: ["80%","80%"],
                        content: '/caiwu/visit/'+data.id+'/edit',
                    })
                }
            });

            $("#addBtn").click(function () {
                layer.open({
                    type: 2,
                    title: "添加",
                    shadeClose: true,
                    area: ["80%","80%"],
                    content: "{{route("crm.visit.create")}}",
                })
            })

        })
    </script>
@endsection
